package com.yj.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.net.URLDecoder;

/**
 * Created by yanlinyang on 2017/4/18.
 */
public class HttpUtil {
    private final  static String CharSet = "UTF-8";

    public static void main(String[] args) {
        doGet("http://hyfw.95306.cn/gateway/hywx/TrainWebClient/hwzzPage.action");
    }


    public static String doGet(String URL) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            client = HttpClients.createDefault();
            HttpGet req = new HttpGet(URL);
            response = client.execute(req);
            String name = response.getFirstHeader("Cookie").getName();
            String value = response.getFirstHeader("Cookie").getValue();
            System.out.println("allHeadersname:"+name);
            System.out.println("allHeadersvalue:"+value);
            HttpEntity entity = response.getEntity();

            return EntityUtils.toString(entity, CharSet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            {
                try {
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (client != null) {
                        client.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    public static JSONObject httpGet(String url){
        //get请求返回结果
        JSONObject jsonResult = null;
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            //发送get请求
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);

            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
                String strResult = EntityUtils.toString(response.getEntity());
                /**把json字符串转换成json对象**/
                jsonResult = JSONObject.fromObject(strResult);
                url = URLDecoder.decode(url, "UTF-8");
            } else {
                System.out.println("get请求提交失败:" + url);
            }
        } catch (IOException e) {
            System.out.println("get请求提交失败:" + url);
            e.printStackTrace();
        }
        return jsonResult;
    }
}
